package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.Communication;

@Service
public class CommunicationDAO extends BaseDAO<Communication, Integer> {

	public RESULT findByMap(Map<String, String> map, PageBean pb){
		StringBuffer hql = new StringBuffer("FROM Communication c WHERE 1=1");

		List<Object> list = null;
		if (null != map) {
			list = new ArrayList<Object>();
			
			String name = map.get(Parm.NAME);
			String sex = map.get(Parm.SEX);
			String company = map.get(Parm.COMPANY);
			String type = map.get(Parm.TYPE);
			String cu = map.get(Parm.CU);
			String oc = map.get(Parm.OC);
			if(isNotNull(name)) {
				hql.append(" AND c.name like ?");
				list.add("%" + name + "%");
			}if(isNotNull(company)) {
				hql.append(" AND c.company like ?");
				list.add("%" + company + "%");
			}if(isNotNull(sex)) {
				hql.append(" AND c.sex = ?");
				list.add(sex);
			}if(isNotNull(type)) {
				hql.append(" AND c.type = ?");
				list.add(Byte.parseByte(type));
			}if(isNotNull(cu)) {
				hql.append(" AND c.cu = ?");
				list.add(Integer.parseInt(cu));
			}if(isNotNull(oc)) {
				hql.append(" AND c.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			
		}
		hql.append(" AND (c.deleted = 0 OR c.deleted IS NULL)");
		hql.append(" ORDER BY c.code DESC");
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
	
	public RESULT update(Communication comm) {
		if (null == comm || null == comm.getCode())
			return RESULT.PARAMETER_ERROR;
		
		Communication old = get(comm.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		
		if(isNotNull(comm.getName())) {
			old.setName(comm.getName());
		}if(isNotNull(comm.getAddress())) {
			old.setAddress(comm.getAddress());
		}if(isNotNull(comm.getCompany())) {
			old.setCompany(comm.getCompany());
		}if(isNotNull(comm.getEmail())) {
			old.setEmail(comm.getEmail());
		}if(isNotNull(comm.getRemark())) {
			old.setRemark(comm.getRemark());
		}if(isNotNull(comm.getPhone())) {
			old.setPhone(comm.getPhone());
		}if(null != comm.getSex()) {
			old.setSex(comm.getSex());
		}if(null != comm.getDeleted()) {
			old.setDeleted(comm.getDeleted());
		}if(null != comm.getCt()) {
			old.setCt(comm.getCt());
		}if(null != comm.getType()) {
			old.setType(comm.getType());
		}if(null != comm.getPhoto()) {
			old.setPhoto(comm.getPhoto());
		}
		
		
		RESULT result = upd(old);
		return result;
	}
	
	public List<Communication> findAllByMap(Map<String, String> map) {
		StringBuffer hql = new StringBuffer("FROM Communication c WHERE 1=1");

		List<Object> list = null;
		if (null != map) {
			list = new ArrayList<Object>();

			String name = map.get(Parm.NAME);
			String sex = map.get(Parm.SEX);
			String company = map.get(Parm.COMPANY);
			String type = map.get(Parm.TYPE);
			String cu = map.get(Parm.CU);
			String oc = map.get(Parm.OC);
			if (isNotNull(name)) {
				hql.append(" AND c.name like ?");
				list.add("%" + name + "%");
			}
			if (isNotNull(company)) {
				hql.append(" AND c.company like ?");
				list.add("%" + company + "%");
			}
			if (isNotNull(sex)) {
				hql.append(" AND c.sex = ?");
				list.add(sex);
			}
			if (isNotNull(type)) {
				hql.append(" AND c.type = ?");
				list.add(Byte.parseByte(type));
			}
			if (isNotNull(cu)) {
				hql.append(" AND c.cu = ?");
				list.add(Integer.parseInt(cu));
			}
			if (isNotNull(oc)) {
				hql.append(" AND c.oc = ?");
				list.add(Integer.parseInt(oc));
			}

		}
		hql.append(" AND (c.deleted = 0 OR c.deleted IS NULL)");
		hql.append(" ORDER BY c.code DESC");
		return find(hql.toString(), list.toArray());
	}
}
